﻿package com.eaglesandcobras.common.data {
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	
	public class XMLLoad extends EventDispatcher {
		private var _loader:URLLoader = new URLLoader();
		public var _xml:XMLList;
		private var _type:String;
		
		public function XMLLoad($path:String, $type:String="item"):void {
			_loader.addEventListener(Event.COMPLETE,xmlLoaded);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
			_loader.load(new URLRequest($path));
			
			_type = $type;
		}
		
		private function xmlLoaded(e:Event):void {
			_xml = new XML(e.target.data)[_type];
			
			_loader.removeEventListener(Event.COMPLETE, xmlLoaded);
			
            this.dispatchEvent(e);
		}
		
		private function handleError(e:IOErrorEvent):void {
			trace (e);
		}
		
		/*****************************
		GET XML
		*****************************/
		public function get data():XMLList {
			return _xml;
		}
	}
}